package com.myx.typhoon.web.bussiness.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.myx.typhoon.business.domain.Article;
import com.myx.typhoon.business.domain.User;
import com.myx.typhoon.business.service.ArticleService;
import com.myx.typhoon.business.service.CommentService;
import com.myx.typhoon.common.util.DateUtil;
import com.myx.typhoon.web.bussiness.domian.result.RequestPageVo;
import com.myx.typhoon.web.bussiness.domian.result.RequestResultVo;
import com.myx.typhoon.web.security.SecurityUtil;

/**  
 * @author myx
 * @createTime 2016年1月7日 下午4:01:17  
 * 
 */

@Controller
@RequestMapping("/article")
public class ArticleController {
	
	@Resource
	private ArticleService articleService;
	
	/**
	 * 查询文章列表
	 * @param pageNum
	 * @param pageSize
	 * @param condition
	 * @param model
	 * @return
	 */
	@RequestMapping("/articlesList")
	public String getArticlesList(
			@RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum ,
			@RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize ,
			@RequestParam(value = "condition",defaultValue = "",required = false) String condition ,
			Model model) {
		int start = (pageNum-1)*pageSize;
		RequestPageVo<Article> page = articleService.findArticlesList(start, pageSize, condition);
		model.addAttribute("articlesList", page.getData());
		model.addAttribute("currentPage", pageNum);
		model.addAttribute("totalPage", (page.getRecordsTotal()+pageSize-1)/pageSize);
		model.addAttribute("condition", condition);
		model.addAttribute("totalRecord", page.getRecordsTotal());
		return "/portal/article/articlesList";
	}
	
	/**
	 * 获得文章详细信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/getArticleDetial")
	public String getArticleDetial(@RequestParam(value="id") Long id,Model model) {
		Article article = articleService.findArticleById(id);
		model.addAttribute("article", article);
		return "/portal/article/articleDetial";
	}
	
	/**
	 * 跳转到文章编辑页面
	 * @return
	 */
	@RequestMapping("/editArticle")
	public String editArticle() {
		return "/portal/article/editArticle";
	}
	
	/**
	 * 保存文章
	 * @param article
	 * @return
	 */
	@RequestMapping("/saveArticle")
	public @ResponseBody RequestResultVo<Article> saveArticle(@ModelAttribute("article") Article article) {
		RequestResultVo<Article> result = new RequestResultVo<Article>();
		User user = SecurityUtil.getCurrentUser();
		result = articleService.saveArticle(article,user.getUserId());
		return result;
	}
}
